package com.reps.util;

import org.dom4j.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lujw on 2018/6/4.
 */
public class TreeUtil {


    public static Map getBywindow(Element node ){
        Map map = new HashMap();
        List<Element> listElement=node.elements();//所有一级子节点的list
        for(Element first : listElement){
           if(first.getName().equals("changes")){
               List<Element> childElement=first.elements();//所有一级子节点的list
               Element winElement = childElement.get(0);//获取最后一个
               String wid = winElement.attributeValue("wid");
               Element control = (Element) winElement.elements().get(0);
               String cid = control.attributeValue("cid");
               map.put("wid",wid);
               map.put("cid",cid);
               break;
           }
        }
        return map;
    }

    /**
     * 根据节点名称获取结点
     * @param node
     * @param childName
     * @return
     */
    public static Element getByNodeName(Element node, String childName ){
        Element res = null;
        List<Element> listElement=node.elements();//所有一级子节点的list
        for(Element first : listElement){
            if(first.getName().equals(childName)){
                res = first;
                break;
            }
        }
        return res;
    }
}
